//
//  OTDR_LocalDataViewController.m
//  OTDR
//
//  Created by Liu on 2024/5/19.
//

#import "OTDR_LocalDataViewController.h"
#import "OTDR_LocalDataCell.h"
#import "OTDR_LF_DrawViewController.h"

@interface OTDR_LocalDataViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic ,strong)UITableView *tableView;
/**
 *<#define#>
 */
@property(nonatomic ,strong)NSArray *filesArray;
@end

@implementation OTDR_LocalDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitleStr:@""];
    self.view.backgroundColor = kWhiteColor;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    [self getAllFile];
}

- (void )getAllFile{
    // 获取Documents目录路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [paths objectAtIndex:0];
    NSString      *documentsDirectory = [document stringByAppendingPathComponent:@"EIFile"];
    // 创建文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
     
    // 获取Documents目录下所有文件
    NSError *error;
    NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error];
    self.filesArray = [NSArray new];
    // 检查错误
    if (!files) {
        NSLog(@"Error reading directory: %@", [error localizedDescription]);
    } else {
        // 输出所有文件
        self.filesArray = files;
    }
    [self.tableView reloadData];
}

#pragma mark -- tableView
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 50;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 11.0,*)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        [_tableView registerClass:[OTDR_LocalDataCell class] forCellReuseIdentifier:@"OTDR_LocalDataCell"];
    }
    return _tableView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SIZE(50);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.filesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OTDR_LocalDataCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"OTDR_LocalDataCell" forIndexPath:indexPath];
    cell.fileName = self.filesArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSString*documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager *fileManager      = [NSFileManager defaultManager];
    NSString      *testDirectory    = [documentsPath stringByAppendingPathComponent:@"EIFile"];
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",testDirectory,self.filesArray[indexPath.row]];
    NSData *filedData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
    
    
//    uint8_t byteSource[66500] = {0};
//    [filedData getBytes:&byteSource length:filedData.length];
//    for (int i =0; i<filedData.length; i++) {
//        printf("%02x ",byteSource[i]);
//    }
    
    
    OTDR_LF_DrawViewController *vc = [OTDR_LF_DrawViewController new];
    vc.drawData = filedData;
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self  presentViewController:vc animated:NO completion:nil];
//    [self.navigationController pushViewController:vc animated:YES];
}
@end
